#pragma once 
#include<netinet/in.h>
#include<arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<iostream>
#include<string>
#include"Common.hpp"
using namespace std;


class InetAddr
{
private:
    void PortNet2Host() //网络转主机
    {
        _port = ::ntohs(_net_addr.sin_port);
    }
    void IpNet2Host() //网络风格ip转字符串
    {
        char ipbuffer[64];
        const char* ip = ::inet_ntop(AF_INET,&_net_addr.sin_addr,ipbuffer,sizeof(ipbuffer));
    }
public:
     InetAddr(){}
     InetAddr(const struct sockaddr_in& addr) //网络->主机
     :_net_addr(addr)
     {
        PortNet2Host();
        IpNet2Host();
     }
     InetAddr(uint16_t port)//主机->网络
     :_port(port)
     {
        _net_addr.sin_family = AF_INET;
        _net_addr.sin_port = htons(_port);
        _net_addr.sin_addr.s_addr = INADDR_ANY;
     }
     struct sockaddr* Netaddr()
     {
           return CONV(&_net_addr);
     }
     socklen_t NetAddrLen()
     {
        return sizeof(_net_addr);
     }

     string Ip()
     {
        return _ip;
     }
    
     uint16_t Port()
     {
        return _port;
     }

     ~InetAddr(){}
private:
    struct sockaddr_in _net_addr;//网络地址
    string _ip; //字符串风格
    uint16_t _port;
};